var addpartner = {
	
	id:null,
	managerid : null,//管理员ID
	type:1,//1、省，2、市，3、区
	parentid:"",
	
	initPage:function(){
		addpartner.initLoad(addpartner.id);
		$("#cancelBtn").click(function(){
			addpartner.backList();
		});
		$("#confirmBtn").click(function(){
			addpartner.saveOrupdate();
		});
	},
	
	initLoad : function(id) {
		addpartner.queryCityList();
		addpartner.getRoleList();
		if(id){
			partnermanager.queryById({"partnerid":id},function(data){
				if(data.data && data.retCode == 0){
					var obj= data.data;
					$("#partnername").val(obj.partnername);
					$("#remark").val(obj.remark);
					$("#city").val(obj.cityid);
					
					var managerVo = obj.managerVo;
					if(managerVo){
						addpartner.managerid = managerVo.id;
						$("#account").val(managerVo.account);
						$("#username").val(managerVo.username);
						$("#roleid").val(managerVo.roleid);
					}
				}
			});
		}
	},
	
	queryCityList : function(){
		var p = {};
		p['type'] = addpartner.type;
		if(addpartner.type > 1){
			var userinfo = util.getCookieValue('userinfo');
			userinfo = JSON.parse(userinfo);
			p['parentid'] = userinfo.cityid;
		}
		partnermanager.queryCityList(p, function(data){
			if(data && data.data && data.data.length > 0){
				var list = data.data;
				var _html = "";
				for(var key in list){
					var obj = list[key];
					_html += '<option value="'+obj.id+'">'+obj.name+'</option>';
				}
				$("#city").html(_html);
			}
		});
	},
	
	getRoleList : function(){
		partnermanager.queryRoleList({}, function(data){
			if(data && data.data){
				var list = data.data;
				var _html = '';
				for(var key in list){
					var o = list[key];
					_html += '<option value="'+o.id+'" '+(o.id==roleid?'selected':'')+' >'+o.name+'</option>';
				}
				$("#roleid").html(_html);
			}
		});
	},
	
	saveOrupdate:function(){
		var partnername = $("#partnername").val();
		var remark = $("#remark").val();
		var cityid = $("#city").val();
		var account = $("#account").val();
		var username = $("#username").val();
		var roleid = $("#roleid").val();
		var password = $("#password").val();
		
		if(!partnername){
			util.alertMessage("请输入名称");
			return;
		}
		if(!cityid){
			util.alertMessage("请选择行政单位");
			return;
		}
		if(!account){
			util.alertMessage("请输入管理员账号");
			return;
		}
		if(!username){
			util.alertMessage("请输入管理员名称");
			return;
		}
		
		if(!roleid){
			util.alertMessage("请选择管理员角色");
			return;
		}
		if(!addpartner.managerid && !password){
			util.alertMessage("请输入管理员登录密码");
			return;
		}
		var params ={
				'partnername':partnername,
				'remark':remark,
				'cityid':cityid,
				'type':addpartner.type,
				'managerVo.username':username,
				'managerVo.account':account,
				'managerVo.roleid':roleid,
				'managerVo.password':password
		};
		if(addpartner.id){
			params['id'] = addpartner.id;
		}
		if(addpartner.managerid){
			params['managerVo.id'] = addpartner.managerid;
		}
		partnermanager.save(params,addpartner.add_successcb,addpartner.add_successcb);
	},
	
	add_successcb:function(data){
		 if(data.retCode == 0){
			 layer.msg(language.operatorSuccess[mainIndex.languageType], {icon: 1});
			 addpartner.backList();
		 }else if(data.retCode == -1){
			 util.alertMessage(language.existAccount[mainIndex.languageType]);
		 }
	},
		
	backList:function(){
		if(addpartner.type==1){
			util.loadContent(util.webpath+'partner/partnerList.html','contentDiv',{},function(){});
		}else if(addpartner.type==2){
			util.loadContent(util.webpath+'partner/partnerList2.html','contentDiv',{},function(){});
		}else if(addpartner.type==3){
			util.loadContent(util.webpath+'partner/partnerList3.html','contentDiv',{},function(){});
		}
	},
}
